/*
 * @Date         : 2022-07-08 21:34:33
 * @LastEditors  : zu1662
 * @LastEditTime : 2024-05-24 15:12:23
 * @Description  : 邮件 controller
 *
 * Copyright © 2022 by zu1662, All Rights Reserved.
 */
import {
  Controller,
  Post,
  Query,
  Param,
  Delete,
  Body,
  Get,
  HttpCode,
  HttpStatus,
  Patch,
} from '@nestjs/common';
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'src/utils/response/response.decorator';
import { PaginationService } from 'src/utils/pagination/service/pagination.service';
import { SMTPEntity } from './smtp.entity';
import { SMTPService } from './smtp.service';
import { AuthJwtGuard } from 'src/auth/auth.decorator';
import { SMTPCreateDto } from './dto/smtp.create.dto';
import { SMTPListDto } from './dto/smtp.list.dto';
import { IResponse } from 'src/utils/response/response.interface';

@ApiTags('smtp')
@Controller({
  version: '1',
  path: 'smtp',
})
export class SMTPController {
  constructor(
    private readonly smtpService: SMTPService,
    private readonly paginationService: PaginationService
  ) {}

  /**
   * 发送邮件
   * @param comment
   */
  @ApiResponse({ status: 200, description: '创建邮件', type: [SMTPEntity] })
  @Response('smtp.create')
  @Post()
  // @AuthJwtGuard()
  create(@Body() data: SMTPCreateDto) {
    return this.smtpService.create(data);
  }

  /**
   * 获取所有邮件记录
   */
  @Response('smtp.list')
  // @AuthJwtGuard()
  @Get()
  @HttpCode(HttpStatus.OK)
  async findAll(@Query() queryParams: SMTPListDto): Promise<IResponse> {
    const { page, perPage, search, sort, availableSearch, availableSort, ...otherParams } =
      queryParams;
    const skip: number = await this.paginationService.skip(page, perPage);

    // 获取分页，排序数据
    const result = await this.smtpService.findAll({
      skip,
      page,
      perPage,
      sort,
      search,
      availableSearch,
      filters: otherParams,
    });
    // 总条数
    const totalData = result.totalData;
    // 根据总条数分页
    const totalPage: number = await this.paginationService.totalPage(totalData, perPage);
    return {
      totalData,
      totalPage,
      currentPage: page,
      perPage,
      availableSearch,
      availableSort,
      data: result.data,
    };
  }

  /**
   * 获取指定邮件记录
   * @param id
   */
  @Response('smtp.detail')
  @HttpCode(HttpStatus.OK)
  // @AuthJwtGuard()
  @Get(':id')
  findById(@Param('id') id) {
    return this.smtpService.findById(id);
  }

  /**
   * 删除邮件记录
   * @param id
   */
  @Response('smtp.delete')
  // @AuthJwtGuard()
  @HttpCode(HttpStatus.OK)
  @Delete(':id')
  deleteById(@Param('id') id) {
    return this.smtpService.deleteById(id);
  }
}
